package com.skcc.cop.mrtaxi.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public final class JsonUtil {

	public static JSONObject intoJsonObject(HashMap<String, String> hashIn) throws JSONException {
		JSONObject reJson = new JSONObject();
		Set<Entry<String, String>> set = hashIn.entrySet();
		Iterator<Entry<String, String>> it = set.iterator();

		while (it.hasNext()) {
			Map.Entry<String, String> e = (Map.Entry<String, String>) it.next();
			reJson.put(e.getKey(), e.getValue());
		}
		return reJson;
	}

	public static JSONObject intoJsonObject(String headerKey, HashMap<String, String> header,String paramSetKey,
			HashMap<String, String> paramSet, String dataSetKey, ArrayList<HashMap<String, String>> dataSetList) throws JSONException {
		JSONObject reJson = new JSONObject();
		reJson.put(headerKey, intoJsonObject(header));
		reJson.put(paramSetKey, intoJsonObject(paramSet));
		
		JSONArray jsonArrayData = new JSONArray();
		for (int i = 0; i < dataSetList.size(); i++) {
			JSONObject tempJson = new JSONObject();
			Set<Entry<String, String>> set = dataSetList.get(i).entrySet();
			Iterator<Entry<String, String>> it = set.iterator();

			while (it.hasNext()) {
				Map.Entry<String, String> e = (Map.Entry<String, String>) it.next();
				tempJson.put(e.getKey(), e.getValue());
			}
			jsonArrayData.put(i, tempJson);
		}
		
		reJson.put(dataSetKey, jsonArrayData);
		return reJson;
	}

	public static String intoJsonString(HashMap<String, String> hashIn) throws JSONException {
		JSONObject reJson = new JSONObject();
		Set<Entry<String, String>> set = hashIn.entrySet();
		Iterator<Entry<String, String>> it = set.iterator();

		while (it.hasNext()) {
			Map.Entry<String, String> e = (Map.Entry<String, String>) it.next();
			reJson.put(e.getKey(), e.getValue());
		}
		return reJson.toString();
	}

	public static JSONObject intoJsonObject(String head, ArrayList<HashMap<String, String>> hashList)
			throws JSONException {
		JSONArray jsonArray = new JSONArray();
		for (int i = 0; i < hashList.size(); i++) {
			JSONObject tempJson = new JSONObject();
			Set<Entry<String, String>> set = hashList.get(i).entrySet();
			Iterator<Entry<String, String>> it = set.iterator();

			while (it.hasNext()) {
				Map.Entry<String, String> e = (Map.Entry<String, String>) it.next();
				tempJson.put(e.getKey(), e.getValue());
			}
			jsonArray.put(i, tempJson);
		}
		JSONObject reArrayJson = new JSONObject();
		reArrayJson.put(head, jsonArray);
		return reArrayJson;
	}

	public static HashMap<String, String> intoHashMap(JSONObject jsonObject) throws JSONException {
		HashMap<String, String> reHash = new HashMap<String, String>();
		Iterator iter = jsonObject.keys();
		while (iter.hasNext()) {
			String key = (String) iter.next();
			String value = jsonObject.getString(key);
			reHash.put(key, value);
		}
		return reHash;
	}
	
	public static HashMap<String, String> intoHashMapKey(String key, JSONObject jsonObject) throws JSONException {
		JSONObject reJson = jsonObject.getJSONObject(key);
		return intoHashMap(reJson);
	}

	public static HashMap<String, String> intoHashMap(JSONObject jsonObjectArray, String head) throws JSONException {
		JSONObject json = new JSONObject(jsonObjectArray.toString());
		JSONObject jsonObject = json.getJSONObject(head);

		HashMap<String, String> reHash = new HashMap<String, String>();
		Iterator iter = jsonObject.keys();
		while (iter.hasNext()) {
			String key = (String) iter.next();
			String value = jsonObject.getString(key);
			reHash.put(key, value);
		}
		return reHash;
	}
	
	public static ArrayList<HashMap<String, String>> intoHashMapListKey(String key, JSONObject jsonInput) throws JSONException {
		JSONArray jsonArray = jsonInput.getJSONArray(key);
		ArrayList<HashMap<String, String>> reHashList = new ArrayList<HashMap<String, String>>();

		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			HashMap<String, String> hashList = new HashMap<String, String>();
			Iterator iter = jsonObject.keys();
			while (iter.hasNext()) {
				String reKey = (String) iter.next();
				String value = jsonObject.getString(reKey);
				hashList.put(reKey, value);
			}
			reHashList.add(hashList);
		}
		return reHashList;
	}

	public static HashMap<String, String> intoHashMap(String jsonString) throws JSONException {
		JSONObject jsonObject = new JSONObject(jsonString);
		HashMap<String, String> reHash = new HashMap<String, String>();
		Iterator iter = jsonObject.keys();
		while (iter.hasNext()) {
			String key = (String) iter.next();
			String value = jsonObject.getString(key);
			reHash.put(key, value);
		}
		return reHash;
	}

	public static ArrayList<HashMap<String, String>> intoHashMapList(JSONObject jsonObjectArray, String head)
			throws JSONException {
		ArrayList<HashMap<String, String>> reHashList = new ArrayList<HashMap<String, String>>();
		JSONObject json = new JSONObject(jsonObjectArray.toString());
		JSONArray jsonArray = json.getJSONArray(head);

		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			HashMap<String, String> hashList = new HashMap<String, String>();
			Iterator iter = jsonObject.keys();
			while (iter.hasNext()) {
				String key = (String) iter.next();
				String value = jsonObject.getString(key);
				hashList.put(key, value);
			}
			reHashList.add(hashList);
		}
		return reHashList;
	}
}
